package com.jy.wms.service;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.*;
import com.jy.wms.dao.WmsShipmentOrderDetailedMapper;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsShipmentOrderMapper;
import io.swagger.models.auth.In;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.annotations.Param;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: WmsShipmentOrderService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Wed Oct 18 11:12:06 GMT+08:00 2017
 */

@Service
public class WmsShipmentOrderService extends BaseService<WmsShipmentOrderMapper, WmsShipmentOrder> {
    @Autowired
    private WmsShipmentOrderDetailedMapper wmsShipmentOrderDetailedMapper;

    /**
     * 插入一条记录
     *
     * @param map
     * @return int
     */
    public Integer baseInsert(Map<String, Object> map) {
        return dao.baseInsert(map);
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 发运订单实体对象
     * @return int
     */
    @Transactional
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "增加订单")
    public Integer baseInsertSelective(WmsShipmentOrder entity) throws SucessException, ServiceException {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // new Date()为获取当前系统时间
        String date = df.format(new Date());
        entity.setCjsj(date);
        entity.setXgsj(date);
        if (CommonInfo.zdsh.getType().equals(entity.getZdsh())) {
            entity.setShr("超级管理员");
        }
        entity.setJhfhsj(df.format(new Date(Long.valueOf(entity.getJhfhsj()).longValue())));
        entity.setYqjhsj(df.format(new Date(Long.valueOf(entity.getYqjhsj()).longValue())));
        //明细集合
        List<WmsShipmentOrderDetailed> lists = entity.getWsodList();
        for (int i = 0; i < lists.size() - 1; i++) {
            for (int j = i + 1; j < lists.size(); j++) {
                if (lists.get(j).getHpid().equals(lists.get(i).getHpid())) {
                    throw new ServiceException("订单明细包含重复货品,添加失败");
                }
            }
        }
        //查询订单编号是否重复(同意货主不可重复)
        Integer e = dao.getCountByBh(entity.getSshzid(), entity.getFybh(), entity.getCkid());
        //判断订单编号是否重复
        if (e <= 0) {
            //保存发运订单
            Integer a = dao.baseInsertSelective(entity);
            Integer b = 0;
            if (a > 0) {
                for (WmsShipmentOrderDetailed wsod : lists) {
                    if (wsod.getScrq() != null && !wsod.getScrq().equals("")) {
                        wsod.setScrq(df.format(new Date(Long.valueOf(wsod.getScrq()).longValue())));
                    }
                    wsod.setSsfyddid(entity.getId());
                    wsod.setCkid(entity.getCkid());
                    wsod.setId(UUIDUtils.uuid());
                    wsod.setCjsj(date);
                }
                //增加订单明细
                b = wmsShipmentOrderDetailedMapper.baseInsert(lists);
                if (b > 0) {
                    throw new SucessException("发运订单添加成功");
                } else {
                    //如果明细添加失败那么回去删掉发运订单
                    Integer c = dao.deleteById(entity.getId());
                    throw new ServiceException("发运订单添加失败");
                }
            } else {
                throw new ServiceException("发运订单添加失败");
            }
        } else {
            throw new ServiceException("发运订单编号重复,添加失败");
        }
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param id 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(String id) {
        return dao.deleteById(id);
        //return dao.baseDeleteBathPrimaryKeys(idList);
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsShipmentOrder>
     */
    public Page<WmsShipmentOrder> baseSelectPage(WmsShipmentOrder entity, String ckid, String hzid) {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        /* if (entity.getCjsj_start() != null && entity.getCjsj_start() != "") {
            entity.setCjsj_start(df.format(new Date(Long.valueOf(entity.getCjsj_start()).longValue())));
        }
        if (entity.getCjsj_end() != null && entity.getCjsj_end() != "") {
            entity.setCjsj_end(df.format(new Date(Long.valueOf(entity.getCjsj_end()).longValue())));
        }
        if (entity.getKhcjsj_start() != null && entity.getKhcjsj_start() != "") {
            entity.setKhcjsj_start(df.format(new Date(Long.valueOf(entity.getKhcjsj_start()).longValue())));
        }
        if (entity.getKhcjsj_end() != null && entity.getKhcjsj_end() != "") {
            entity.setKhcjsj_end(df.format(new Date(Long.valueOf(entity.getKhcjsj_end()).longValue())));
        }
       if (entity.getJhfhsj_start() != null && entity.getJhfhsj_start() != "") {
            entity.setJhfhsj_start(df.format(new Date(Long.valueOf(entity.getJhfhsj_start()).longValue())));
        }
        if (entity.getJhfhsj_end() != null && entity.getJhfhsj_end() != "") {
            entity.setJhfhsj_end(df.format(new Date(Long.valueOf(entity.getJhfhsj_end()).longValue())));
        }
        if (entity.getYqjhsj_start() != null && entity.getYqjhsj_start() != "") {
            entity.setYqjhsj_start(df.format(new Date(Long.valueOf(entity.getYqjhsj_start()).longValue())));
        }
        if (entity.getYqjhsj_end() != null && entity.getYqjhsj_end() != "") {
            entity.setYqjhsj_end(df.format(new Date(Long.valueOf(entity.getYqjhsj_end()).longValue())));
        }*/
        PageHelper.startPage(entity.getPage(), entity.getRows());
        return (Page<WmsShipmentOrder>) dao.baseSelectPage(entity, ckid, hzid);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsShipmentOrder> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "修改订单")
    public Integer baseUpdateByPrimaryKeySelective(WmsShipmentOrder entity) throws SucessException, ServiceException {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dd = new Date();
        // new Date()为获取当前系统时间
        String date = df.format(dd);

        if (entity.getJhfhsj() != null && entity.getJhfhsj() != "") {
            entity.setJhfhsj(df.format(new Date(Long.valueOf(entity.getJhfhsj()).longValue())));
        }
        if (entity.getYqjhsj() != null && entity.getYqjhsj() != "") {
            entity.setYqjhsj(df.format(new Date(Long.valueOf(entity.getYqjhsj()).longValue())));
        }
        WmsShipmentOrder wmsShipmentOrder = dao.baseSelectByPrimaryKey(entity.getId());//通过id查询对象
        if (wmsShipmentOrder.getShzt().equals(CommonInfo.shipmentOrder_ysh.getType())) {
            throw new ServiceException("订单已审核，不可以编辑");
        } else {
            Integer e = 0;
            if (wmsShipmentOrder.getFybh().equals(entity.getFybh())) {
                entity.setFybh("");
            } else {
                //查询订单编号是否重复(同一货主不可重复)
                e = dao.getCountByBh(entity.getSshzid(), entity.getFybh(), entity.getCkid());
            }
            if (e <= 0) {
                //修改发运订单
                Integer a = dao.baseUpdateByPrimaryKeySelective(entity);
                if (a > 0) {
                    throw new SucessException("发运订单修改成功");
                } else {
                    throw new ServiceException("发运订单修改失败");
                }
            } else {
                throw new ServiceException("订单编号重复，修改失败");
            }
        }
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsShipmentOrder entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }

    /**
     * 获取客户信息
     *
     * @param hzid
     * @param ckid
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "获取客户信息")
    public List<WmsCustomerFile> getCusInfo(String hzid, String ckid, String khbh) throws ServiceException {
        try {
            return dao.getCusInfo(hzid, ckid, khbh);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取客户信息", e);
        }

    }

    /**
     * 获取货主下的货品
     *
     * @param hzid 货主id
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "获取货主下的货品")
    public List<WmsGoods> getGoodsByHzid(String hzid, String ckid, String bh) throws ServiceException {
        try {
            return dao.getGoodsByHzid(hzid, ckid, bh);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取货品", e);
        }
    }

    /**
     * 获取货主下的库位
     *
     * @param hzid
     * @param ckid
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "获取库位")
    public List<WmsLibraryPosition> getLiLibraryPosition(String hzid, String ckid, String mc) throws ServiceException {
        try {
            return dao.getLiLibraryPosition(hzid, ckid, mc);
        } catch (Exception e) {

            throw new ServiceException("获取库位", e);
        }

    }

    /**
     * 获取库存
     *
     * @param ckid
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "获取库存")
    public List<WmsStock> getWmsStock(String hpid, String ckid, String hppc) throws ServiceException {
        try {
            return dao.getWmsStock(hpid, ckid, hppc);
        } catch (Exception e) {

            throw new ServiceException("获取库存", e);
        }
    }

    @BaseServiceDescribe(moduleName = "发运订单", btnName = "获取库存")
    public List<WmsStock> getWmsStockScrq(String hpid, String ckid, String scrq) throws ServiceException {
        try {
            return dao.getWmsStockScrq(hpid, ckid, scrq);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取库存", e);
        }
    }


    /**
     * 作废时修改是否有效的状态
     *
     * @param idList 运单id
     * @param zfr    作废人
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "作废发运订单")
    public Integer updateSfyx(List<String> idList, String zfr) throws SucessException, ServiceException {
        List<WmsShipmentOrder> list = dao.getOrderList(idList);
        List<String> list2 = new ArrayList<String>();
        for (WmsShipmentOrder wpo : list) {
            //未审核（只能作废审核状态为“未审核”下的发运订单 ，将未审核的发运订单标记为无效。）
            if (CommonInfo.shipmentOrder_wsh.getType().equals(wpo.getShzt())) {
                //只能作废一次（作废是否有效状态为 有效 的记录）
                if (CommonInfo.shipmentOrder_sfyx_yx.getType().equals(wpo.getSfyx())) {
                    list2.add(wpo.getId());
                }
            }
        }
        if (list2.size() > 0) {
            //修改发运订单
            Integer a = dao.updateSfyx(list2, zfr);
            if (a > 0) {
                throw new SucessException("选中" + idList.size() + "条记录，作废成功" + a + "条记录");
            } else {
                throw new ServiceException("发运订单作废失败");
            }
        } else {
            throw new ServiceException("选中的记录已审核或已作废，不可以进行此操作");
        }
    }


    /**
     * 标记发运完成
     *
     * @param bjr  标记人
     * @param bjyy 标记原因
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "标记发运订单")
    @Transactional
    public Integer updateZt(List<String> idList, String bjr, String bjyy) throws SucessException, ServiceException {
        List<WmsShipmentOrder> list = dao.getOrderList(idList);
        List<String> list2 = new ArrayList<String>();
        for (WmsShipmentOrder wpo : list) {
            //部分发运（	只能操作部分发运下的发运订单）
            if ("3".equals(wpo.getFyzt())) {
                list2.add(wpo.getId());
            }
        }
        if (list2.size() > 0) {
            //修改发运订单
            Integer a = dao.updateZt(list2, bjr, bjyy);
            Integer b = dao.updateMxZt(list2);//修改明细
            if (a > 0) {
                throw new SucessException("选中" + idList.size() + "条记录，标记成功" + a + "条记录");
            } else {
                throw new ServiceException("发运订单标记失败");
            }
        } else {
            throw new ServiceException("选中的记录不包含部分发运状态的订单，不可以标记");
        }
    }


    /**
     * 审核发运订单
     *
     * @param idList 发运订单id
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "审核发运订单")
    @Transactional
    public Integer updateShzt(List<String> idList, String shr) throws SucessException, ServiceException {
        List<WmsShipmentOrder> list = dao.getOrderList(idList);
        List<String> list2 = new ArrayList<String>();
        for (WmsShipmentOrder wpo : list) {
            //未审核
            if (CommonInfo.shipmentOrder_wsh.getType().equals(wpo.getShzt())) {
                list2.add(wpo.getId());
            }
        }
        if (list2.size() > 0) {
            Integer a = dao.updateShzt(list2, shr);
            if (a > 0) {
                throw new SucessException("选中" + idList.size() + "条记录，审核成功" + a + "条记录");
            } else {
                throw new ServiceException("发运订单审核失败");
            }
        } else {
            throw new ServiceException("选中的记录已经全部审核，不可以再次审核");
        }

    }


    /**
     * 查看获取发运订单
     *
     * @param
     * @return
     */
    /*@BaseServiceDescribe(moduleName = "发运订单", btnName = "查看发运订单")
    public WmsShipmentOrder getWmsShipmentOrder() throws SucessException, ServiceException {
       WmsShipmentOrder wmsShipmentOrder= dao.getWmsShipmentOrder();
       if(wmsShipmentOrder!=null){
           throw new SucessException("发运订单审核");
       } else {
              throw new ServiceException("发运订单审核");
       }
    }*/
    @BaseServiceDescribe(moduleName = "发运订单", btnName = "查询发运订单及明细")
    public JSONObject baseSelectByPrimaryKey(String id,String hzid,String ckid) throws ServiceException {
        try {
            JSONObject jb = new JSONObject();
            //发运订单
            WmsShipmentOrder wpo = dao.baseSelectByPrimaryKey(id);
            JSONObject wmsShipmentOrder = JSONObject.fromObject(wpo);
            //发运订单明细
            List<WmsShipmentOrderDetailed> list = dao.wmsShipmentOrderDetailedList(id,hzid,ckid);
            JSONArray wmsShipmentOrderDetailed = JSONArray.fromObject(list);
            jb.put("wmsShipmentOrder", wmsShipmentOrder);
            jb.put("wmsShipmentOrderDetailed", wmsShipmentOrderDetailed);
            return jb;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询发运订单及明细", e);
        }
    }

    @BaseServiceDescribe(moduleName = "发运订单", btnName = "查询明细集合")
    public JSONArray getDetailList(String id,String hzid,String ckid) throws ServiceException {
        try {

            //发运订单明细
            List<WmsShipmentOrderDetailed> list = dao.wmsShipmentOrderDetailedList(id,hzid,ckid);
            return JSONArray.fromObject(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询发运订单及明细", e);
        }
    }

    @BaseServiceDescribe(moduleName = "发运订单", btnName = "导入发运订单")
    public void importExcle(HttpSession session, String filepath) throws ServiceException, SucessException, Exception {
        ImportParams params = new ImportParams();
        params.setNeedSave(false);
        params.setTitleRows(0);
        params.setHeadRows(1);
        params.setDataHanlder(new ShipmentOrderMapImportHanlder());
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        //仓库编号
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        //货主id
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String date = df.format(d);
        Calendar cl = Calendar.getInstance();
        cl.setTime(d);
        cl.add(Calendar.DATE, 3);
        //预期交货时间
        String yqjhsj = df.format(cl.getTime());
        JSONObject jb = JSONObject.fromObject(filepath);
        URL url = new URL((String) jb.get("filepath"));
        URLConnection conn = url.openConnection();
        List<Map<String, Object>> result = ExcelImportUtil.importExcel(
                conn.getInputStream(),
                Map.class, params);
        for (int i = 0; i < result.size(); i++) {
            result.get(i).put("num", i + 2);
        }
        //表格去重
        removeDuplicate(result);
        //数据库去重
        removeDataDuplicate(result, hzid, ckid);
        //判断表格中是否有错误数据
        removeError(result, session);
        Map<String, Map> map = new HashMap<String, Map>();
        List list = new ArrayList();
        for (int i = 0; i < result.size(); i++) {
            //根据名称，货主查找客户对象
            WmsCustomerFile wmsCustomerFile = dao.getCustomerFile((String) result.get(i).get("khmc").toString(), (String) result.get(i).get("khbh").toString(), hzid, ckid);
            //类型对象
            WmsOwnerOrdertype wmsOwnerOrdertype = dao.getWmsOwnerOrderType((String) result.get(i).get("lx").toString(), hzid, ckid);
            //货品对象
            WmsGoods wmsGoods = dao.getGoods((String) result.get(i).get("hpbh"), ckid);
            result.get(i).put("id", UUIDUtils.uuid());
            result.get(i).put("sshzid", hzid);
            result.get(i).put("khid", wmsCustomerFile.getId());
            result.get(i).put("lxid", wmsOwnerOrdertype.getId());
            result.get(i).put("khddh", result.get(i).get("dh"));
            result.get(i).put("khcjsj", result.get(i).get("ddrq"));
            result.get(i).put("cjsj", date);
            result.get(i).put("cjr", userinfo.getUser().getName());
            result.get(i).put("ckid", ckid);
            result.get(i).put("yqjhsj", yqjhsj); //要求交货时间
            result.get(i).put("hpid", wmsGoods.getId()); //货品id
            result.get(i).put("zdw",wmsGoods.getZdw());//主单位
            result.get(i).put("yqsl", result.get(i).get("hpsl")); //货品数量（主单位数量）
            if(result.get(i).get("hpsl")!=null &&result.get(i).get("hpsl")!="" && wmsGoods.getZdwsl()!=null &&wmsGoods.getZdwsl()!=""){
                Integer num1=  Integer.parseInt((String) result.get(i).get("hpsl"))/ Integer.parseInt(wmsGoods.getZdwsl());
                Integer num2= Integer.parseInt((String) result.get(i).get("hpsl"))% Integer.parseInt(wmsGoods.getZdwsl());
                result.get(i).put("yqxdwsl", num1+"箱"+num2+"件"); //货品数量（箱单位数量）
            }
            result.get(i).put("mxid", UUIDUtils.uuid());
            result.get(i).put("lsje", result.get(i).get("hplsje"));
            //自动审核时默认已审核
            if (CommonInfo.zdsh.getType().equals(wmsOwnerOrdertype.getZdsh())) {
                result.get(i).put("shzt", "1");
                result.get(i).put("shr", "超级管理员");
            } else {
                //否则默认为未审核
                result.get(i).put("shzt", "0");
                result.get(i).put("shr", "");
            }
            //订单保存
            Integer a = dao.baseInsert(result.get(i));
            //保存订单明细
            Integer b = wmsShipmentOrderDetailedMapper.baseInsertByMap(result.get(i));
            System.out.println(result.get(i));
        }
        throw new SucessException("发运订单导入成功");
    }


    /**
     * 查找有错的记录
     *
     * @param lis
     */
    public void removeError(List<Map<String, Object>> lis, HttpSession session) throws ServiceException {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        //仓库编号
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        //货主id
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < lis.size(); i++) {
            //根据名称，货主查找客户对象
            WmsCustomerFile wmsCustomerFile = dao.getCustomerFile((String) lis.get(i).get("khmc").toString(), (String) lis.get(i).get("khbh").toString(), hzid, ckid);
            //类型对象
            WmsOwnerOrdertype wmsOwnerOrdertype = dao.getWmsOwnerOrderType((String) lis.get(i).get("lx").toString(), hzid, ckid);
            //货品对象
            WmsGoods wmsGoods = dao.getGoods((String) lis.get(i).get("hpbh"), ckid);
            if (wmsCustomerFile == null || wmsOwnerOrdertype == null || wmsGoods == null) {
                sb.append("第" + lis.get(i).get("num") + "行，订单号为" + lis.get(i).get("dh") + "的记录");
            }
            if (wmsCustomerFile == null) {
                sb.append(",客户名称或客户编号不存在");
            }
            if (wmsOwnerOrdertype == null) {
                sb.append(",单据类型不存在");
            }
            if (wmsGoods == null) {
                sb.append(",货品编号不存在；");
            }
        }
        if (sb != null && !"".equals(sb) && sb.length() > 0) {
            throw new ServiceException("表格中" + sb);
        }
    }

    /**
     * 报表内去重
     */
    public void removeDuplicate(List<Map<String, Object>> lis) throws ServiceException {
        //报表内不允许存在相同订单号下有相同货品的情况
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < lis.size() - 1; i++) {
            for (int j = i + 1; j < lis.size(); j++) {
                //JSONObject jb = new JSONObject();
                if (lis.get(j).get("dh").equals(lis.get(i).get("dh")) && lis.get(j).get("hpbh").equals(lis.get(i).get("hpbh"))) {
                    sb.append("第" + lis.get(j).get("num") + "行，订单号：" + lis.get(j).get("dh") + "，货品编号:" + lis.get(j).get("hpbh") + ",");
                    lis.remove(j);
                    j--;
                }
            }
        }
        if (sb != null && !"".equals(sb) && sb.length() > 0) {
            throw new ServiceException("表格中" + sb + "货品重复，导入失败");
        }

    }

    /**
     * 数据库去重
     *
     * @param lis
     * @param hzid
     * @param ckid
     * @throws ServiceException
     */
    public void removeDataDuplicate(List<Map<String, Object>> lis, String hzid, String ckid) throws ServiceException {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < lis.size(); i++) {
            JSONObject jb = new JSONObject();
            //查询订单编号是否重复(同意货主不可重复)
            Integer e = dao.getCountByBh(hzid, (String) lis.get(i).get("dh"), ckid);
            if (e > 0) {
                sb.append("第" + lis.get(i).get("num") + "行，订单号：" + lis.get(i).get("dh") + ",");
            }
        }
        if (sb != null && !"".equals(sb) && sb.length() > 0) {
            throw new ServiceException(sb + "数据库包含重复订单号,导入失败");
        }
    }


    /**
     * 获取订单（PO）类型
     *
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单管理", btnName = "获取订单（PO）类型")
    public List<WmsOwnerOrdertype> getPoLx(String hzid, String ckid, String ddlx) throws ServiceException {
        try {
            return dao.getPoLx(hzid, ckid, ddlx);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取订单（PO）类型", e);
        }
    }


    /**
     * 根据id查找客户对象
     *
     * @param id 客户id
     * @return WmsCustomerFile
     */
    @BaseServiceDescribe(moduleName = "发运订单管理", btnName = "获取客户对象")
    public WmsCustomerFile getCusById(@Param("id") String id) throws ServiceException {
        try {
            return dao.getCusById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取订单（PO）类型", e);
        }
    }


    /**
     * 获取字段对应的列名(发运主表)
     * @param hzid 货主id
     * @param ckid 仓库id
     * @return
     */
    @BaseServiceDescribe(moduleName = "发运订单管理", btnName = "获取字段对应的列名")
    public List<WmsColumnName> getFyColumnName(String hzid,String ckid)throws ServiceException {
        try {
            return dao.getFyColumnName(hzid,ckid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取发运主表字段对应的列名", e);
        }
    }

    /**
     * 获取字段对应的列名（发运明细）
     * @param hzid 货主id
     * @param ckid 仓库id
     * @return
     */
    public List<WmsColumnName> getFymxColumnName(String hzid,String ckid)throws ServiceException {
        try {
            return dao.getFymxColumnName(hzid,ckid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取发运明细字段对应的列名", e);
        }
    }


    /**
     *  根据列名查找该列对应的字典
     * @param zcloumn 列名
     * @param hzid 货主
     * @param ckid 仓库
     * @return
     */
    public List<WmsCustomDictionary> getDictionary(String zcloumn,String hzid,String ckid) throws ServiceException {
        try {
            return dao.getDictionary(zcloumn,hzid,ckid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取列名对应的字典", e);
        }
    }

    /**
     * 根据货品id 查询货品库存
     * @param hpid 货品id
     * @param ckid 仓库id
     * @return
     */
    public Page<WmsStock> getGoodsStock( String hpid,int page,int rows, String ckid) throws ServiceException {
        try {
            PageHelper.startPage(page, rows);
            return (Page<WmsStock>) dao.getGoodsStock(hpid,ckid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取货品的可用库存", e);
        }
    }


}
